package cn.iocoder.yudao.module.erp.controller.admin.dpitem;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.erp.controller.admin.dpitem.vo.*;
import cn.iocoder.yudao.module.erp.dal.dataobject.dpitem.DpItemDO;
import cn.iocoder.yudao.module.erp.service.dpitem.DpItemService;

@Tag(name = "管理后台 - ERP总制单货号详情")
@RestController
@RequestMapping("/erp/dp-item")
@Validated
public class DpItemController {

    @Resource
    private DpItemService dpItemService;

    @PostMapping("/create")
    @Operation(summary = "创建ERP总制单货号详情")
    // @PreAuthorize("@ss.hasPermission('erp:dp-item:create')")
    public CommonResult<Long> createDpItem(@Valid @RequestBody DpItemSaveReqVO createReqVO) {
        return success(dpItemService.createDpItem(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新ERP总制单货号详情")
    // @PreAuthorize("@ss.hasPermission('erp:dp-item:update')")
    public CommonResult<Boolean> updateDpItem(@Valid @RequestBody DpItemSaveReqVO updateReqVO) {
        dpItemService.updateDpItem(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除ERP总制单货号详情")
    @Parameter(name = "id", description = "编号", required = true)
    // @PreAuthorize("@ss.hasPermission('erp:dp-item:delete')")
    public CommonResult<Boolean> deleteDpItem(@RequestParam("id") Long id, @RequestParam("no") String no) {
        dpItemService.deleteDpItem(id, no);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得ERP总制单货号详情")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    // @PreAuthorize("@ss.hasPermission('erp:dp-item:query')")
    public CommonResult<DpItemRespVO> getDpItem(@RequestParam("id") Long id) {
        DpItemDO dpItem = dpItemService.getDpItem(id);
        return success(BeanUtils.toBean(dpItem, DpItemRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得ERP总制单货号详情分页")
    // @PreAuthorize("@ss.hasPermission('erp:dp-item:query')")
    public CommonResult<PageResult<DpItemRespVO>> getDpItemPage(@Valid DpItemPageReqVO pageReqVO) {
        PageResult<DpItemDO> pageResult = dpItemService.getDpItemPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DpItemRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出ERP总制单货号详情 Excel")
    // @PreAuthorize("@ss.hasPermission('erp:dp-item:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDpItemExcel(@Valid DpItemPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DpItemDO> list = dpItemService.getDpItemPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "ERP总制单货号详情.xls", "数据", DpItemRespVO.class,
                        BeanUtils.toBean(list, DpItemRespVO.class));
    }

}